﻿using Prism.Services.Dialogs;
using System.Windows;
using WPF.Core;
using WPF.Zero.Controls;
using WPF.Zero.ViewModels;

namespace WPF.Zero.Views
{
    public partial class MainView : Window
    {
        private readonly IHostDialogService dialog;

        public MainView(IHostDialogService dialog)
        {
            InitializeComponent();
            this.dialog = dialog;

            ColorZone.MouseMove += (s, e) =>
            {
                if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
                    this.DragMove();
            };

            ColorZone.MouseDown += (s, e) =>
            {
                if (e.ClickCount > 1)
                {
                    this.SetWindowState();
                }
            };
        }

        private void BtnMinClick(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void BtnMaxClick(object sender, RoutedEventArgs e)
        {
            this.SetWindowState();
        }

        private async void BtnCloseClick(object sender, RoutedEventArgs e)
        {
            var dialogResult = await dialog.Question("温馨提示", "确认退出系统？");
            if (dialogResult.Result != ButtonResult.OK) return;

            this.Close();
        }

        private void SetWindowState()
        {
            if (this.WindowState == WindowState.Maximized)
                this.WindowState = WindowState.Normal;
            else
            {
                this.MaxHeight = SystemParameters.WorkArea.Height;
                this.WindowState = WindowState.Maximized;
            }
        }

        private void OnCloseButtonClick(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource != null && e.OriginalSource is TabCloseItem tabItem)
            {
                if (this.DataContext is MainViewModel viewModel)
                    viewModel.RemoveView(tabItem.Content);
            }
        }
    }
}
